package cn.shen.edit.ftp;


import cn.shen.common.util.WebResultUtil;
import cn.shen.common.vo.WebResult;
import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class FtpServices {

    /** 上传视频*/
    public WebResult uploadFileToFtp(@RequestBody MultipartFile file, HttpServletResponse response) throws Exception {

        String LOCAL_CHARSET = "UTF-8";
        FTPClient ftpClient = this.initFtpClient();
        InputStream inputStream = null;
        String filePath = "optftp_data";
        try {
            inputStream = file.getInputStream();
            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
                    "OPTS UTF8", "ON"))) { //开启服务器对UTF-8的支持，如果服务器支持就用UTF-8编码，否则就使用本地编码（GBK）.
                LOCAL_CHARSET = "UTF-8";
            }
            ftpClient.setControlEncoding(LOCAL_CHARSET);
            response.setHeader("Access-Control-Allow-Origin","*" );
            String b = file.getName();
            String a = file.getOriginalFilename();
            ftpClient.enterLocalPassiveMode();
            ftpClient.changeWorkingDirectory(filePath);
            ftpClient.makeDirectory(file.getOriginalFilename());
            ftpClient.changeWorkingDirectory(filePath);

            ftpClient.storeFile(new String(file.getOriginalFilename().getBytes("UTF-8"),
                    "ISO-8859-1"),inputStream);
            ftpClient.storeFile(file.getOriginalFilename(),inputStream);
            inputStream.close();
            ftpClient.logout();
            return WebResultUtil.genSuccessResult();
        } catch (Exception e) {
            e.printStackTrace();
            return WebResultUtil.genErrorResult();
        }finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    log.info("销毁失败upload{}", e);
                    e.printStackTrace();
                }
            }
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.info("关闭流失败upload{}", e);
                    e.printStackTrace();
                }
            }
        }
    }

    //    获得客户端
    public FTPClient initFtpClient()  {
        FTPClient ftpClient = new FTPClient();
        try {
//            System.out.println(inputStream  +file);
            ftpClient.connect("192.168.89.130",21);
            ftpClient.login("cina","cs110120");
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.setControlEncoding("utf-8");
            int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                log.info("connect failed...ftp服务器" + "192.168.163.130" + ":" + 21);
            }
            log.info("connect successful...ftp服务器" + "192.168.163.130" + ":" + 21);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return ftpClient;
    }

}
